const AV = require('leanengine');
const wxpay = require('./wxpay');
const {
    validateSign,
    handleError,
} = require('./utils');

class Order extends AV.Object {
    get number() { return this.get('number'); }  //订单编号
    set number(value) { this.set('number', value); }

    get itemPrice() { return this.get('itemPrice'); }   //总价
    set itemPrice(value) { this.set('itemPrice', value); }

    get giver() { return this.get('giver'); }    //赠送者
    set giver(value) { this.set('giver', value); }

    get name() { return this.get('name'); }   //商品类型——名称
    set name(value) { this.set('name', value); }

    get status() { return this.get('status'); }    //订单状态
    set status(value) { this.set('status', value); }

    get ip() { return this.get('ip'); }   //客户端ip
    set ip(value) { this.set('ip', value); }

    get tradeType() { return this.get('tradeType'); }  //交易类型
    set tradeType(value) { this.set('tradeType', value); }

    get prepayId() { return this.get('prepayId'); }   //预支付交易会话标识
    set prepayId(value) { this.set('prepayId', value); }

    get blessings() { return this.get('blessings'); }  //祝福语
    set blessings(value) { this.set('blessings', value); }

    get count() { return this.get('count'); }     //数量
    set count(value) { this.set('count', value); }

    get startColor() { return this.get('startColor'); }   //起始渐变色
    set startColor(value) { this.set('startColor', value); }

    get endColor() { return this.get('endColor'); }    //结束渐变色
    set endColor(value) { this.set('endColor', value); }

    get buttonColor() { return this.get('buttonColor'); }    //按钮颜色
    set buttonColor(value) { this.set('buttonColor', value); }

    get picture() { return this.get('picture'); }    //商品类型图片
    set picture(value) { this.set('picture', value); }

    get titleDescribe() { return this.get('titleDescribe'); }    //标题描述
    set titleDescribe(value) { this.set('titleDescribe', value); }

    get contentDescribe() { return this.get('contentDescribe'); }   //内容描述
    set contentDescribe(value) { this.set('contentDescribe', value); }

    get direction() { return this.get('direction'); }  //渐变色方向
    set direction(value) { this.set('direction', value); }

    get itemPriceDescribe() { return this.get('itemPriceDescribe'); }   //商品价格描述
    set itemPriceDescribe(value) { this.set('itemPriceDescribe', value); }

    get itemTypeId() { return this.get('itemTypeId'); }   //商品类型id
    set itemTypeId(value) { this.set('itemTypeId', value); }

    get itemPriceId() { return this.get('itemPriceId'); }   //商品价格id
    set itemPriceId(value) { this.set('itemPriceId', value); }

    get minItemPrice() { return this.get('minItemPrice'); }   //价格价位区间最小值
    set minItemPrice(value) { this.set('minItemPrice', value); }

    get maxItemPrice() { return this.get('maxItemPrice'); }   //价格价位区间最大值
    set maxItemPrice(value) { this.set('maxItemPrice', value); }

    place() {
        return new Promise((resolve, reject) => {
            // 参数文档： https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1
            wxpay.createUnifiedOrder({
                openid: this.giver.get('authData').lc_weapp.openid,
                body: this.name,
                out_trade_no: this.number,
                total_fee: this.itemPrice,
                spbill_create_ip: this.ip,
                notify_url: process.env.WEIXIN_NOTIFY_URL,
                trade_type: this.tradeType,
            }, function(err, result) {
                console.log(err, result);
                if (err) return reject(err);
                return resolve(result);
        });
    }).then(handleError).then(validateSign).then(({
            prepay_id,
        }) => {
            this.prepayId = prepay_id;
        return this.save();
    });
    }
}
AV.Object.register(Order);

module.exports = Order;